/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.info.tech;

import net.xfok.info.tech.InfoItem.Info;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

/**
 * @author YangHua
 *
 */
public class InfoManage {
	
	public static boolean checkNet(Context context){
		boolean result=false;
		ConnectivityManager cm=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netinfo = cm.getActiveNetworkInfo();
		if ( netinfo !=null&&netinfo.isConnected() ) {
			result=true;
			Log.i( context.getClass().getName(), "The net was connected" );
		}else{
			Toast.makeText(context,context.getResources().getText(R.string.dialog_message_net_not), Toast.LENGTH_SHORT).show();		
		}
		return result;
	}
	
	 public static void addInfoItem(InfoItem info,Context context) {
		 ContentValues values = new ContentValues();
		 values.put(InfoItem.Info.INFO_COLUMN_INFO_TITLE, info.getTitle());
		 values.put(InfoItem.Info.INFO_COLUMN_INFO_ID, info.getInfoId());
		 values.put(InfoItem.Info.INFO_COLUMN_INFO_DATE, info.getDate());
		 values.put(InfoItem.Info.INFO_COLUMN_INFO_CONTENT, info.getContent());
		 context.getContentResolver().insert(InfoItem.Info.CONTENT_URI, values);
   	 } 
	 
	 public static void delInfoItem(InfoItem info,Context context){
    	 Log.i(context.getClass().getName(), "_id="+info.getId());
    	 context.getContentResolver().delete(InfoItem.Info.CONTENT_URI, InfoItem.Info.INFO_COLUMN_ID+"="+info.getId(), null);
     }
	 
	 public static void delInfoItem(String date,Context context){
		 String where=Info.INFO_COLUMN_INFO_DATE+"<'"+date+"'";
		 Log.i(context.getClass().getName(), where);
		 context.getContentResolver().delete(InfoItem.Info.CONTENT_URI, where, null);
	 }
	 
	 public static InfoItem getInfoItem(Cursor cur){
    	 InfoItem infoItem=null;
    	 if(cur!=null){
	    	 int id = cur.getInt(cur.getColumnIndex(InfoItem.Info.INFO_COLUMN_ID));
	         String title=cur.getString(cur.getColumnIndex(InfoItem.Info.INFO_COLUMN_INFO_TITLE));
	         String infoId = cur.getString(cur.getColumnIndex(InfoItem.Info.INFO_COLUMN_INFO_ID));
	         String date=cur.getString(cur.getColumnIndex(InfoItem.Info.INFO_COLUMN_INFO_DATE));
	         String content=cur.getString(cur.getColumnIndex(InfoItem.Info.INFO_COLUMN_INFO_CONTENT));
	         infoItem=new InfoItem(id,title,infoId,date,content);
    	 }
         return infoItem;
     }
	
}
